package com.flyou.ccnucenter.service;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.widget.ImageButton;
import android.widget.RemoteViews;

import com.flyou.ccnucenter.AppWidget;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.domain.News;
import com.flyou.ccnucenter.utils.UrlUtils.NewsUrl;
import com.flyou.ccnucenter.utils.UrlUtils.main.NewsUrlPaster;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class WidgetService extends Service {
    RemoteViews views;
    private ImageButton ball, book, calendar;
    private Timer timer;

    public WidgetService() {
    }

    @Override
    public IBinder onBind(Intent intent) {

        throw new UnsupportedOperationException("Not yet implemented");
    }

    //10分钟更新一次
    @Override
    public void onCreate() {
        views = new RemoteViews(getPackageName(), R.layout.widget);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                new getDate().execute(NewsUrl.getSchoolMessageUrl(0),NewsUrl.getEduNewsUrl(0),NewsUrl.getSchoolNewsUrl(0));
            }
        }, 0, 600000);
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        timer.cancel();
        timer = null;
        super.onDestroy();
    }





    class getDate extends AsyncTask<String,Map<String,List<News>>,Map<String,List<News>>> {
        @Override
        protected Map<String,List<News>> doInBackground(String... params) {
            Map<String,List<News>> map=new HashMap<>();
            map.put("ball",NewsUrlPaster.getAllNews(params[0]));
            map.put("book",NewsUrlPaster.getAllNews(params[1]));

            return map;
        }

        @Override
        protected void onPostExecute(Map<String,List<News>> newses) {
            News henuMessage = newses.get("ball").get(0);
            News eduMessage = newses.get("book").get(0);
//            News henuNews = newses.get("calendar").get(0);
            views = new RemoteViews(getPackageName(), R.layout.widget);
            views.setTextViewText(R.id.widget_ball_list, henuMessage.getTitle());
            views.setTextViewText(R.id.widget_book_list, eduMessage.getTitle());
//            views.setTextViewText(R.id.widget_calendar_list, henuNews.getTitle());

            Intent ballClick = new Intent("ball");
            PendingIntent ballPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
                    ballClick, 0);
            views.setOnClickPendingIntent(R.id.ball, ballPendingIntent);

            Intent bookClick = new Intent("book");
            PendingIntent bookPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
                    bookClick, 0);
            views.setOnClickPendingIntent(R.id.book, bookPendingIntent);

            Intent calendarClick = new Intent("calendar");
            PendingIntent calendarPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
                    calendarClick, 0);
            views.setOnClickPendingIntent(R.id.calendar, calendarPendingIntent);


            ComponentName componentName = new ComponentName(getApplicationContext(), AppWidget.class);
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
            appWidgetManager.updateAppWidget(componentName, views);
            super.onPostExecute(newses);
        }
    }
}
